import { Component, OnInit } from '@angular/core';
import { FetchService } from '../../../core/net/fetch.service';
import { NzCascaderOption, NzMessageService } from 'ng-zorro-antd';
import * as moment from 'moment';

@Component({
    selector: 'app-water-price',
    templateUrl: './water-price.component.html',
    styleUrls: ['./water-price.component.scss']
})
export class WaterPriceComponent implements OnInit {
    gateIds: string[] | null = null;
    contentTitle: string;
    year = new Date();
    loading = true;
    pid: string = '0';
    tableData = [];
    isEditable = false;
    tableWidthConfig = ['100px', '150px', '150px', '150px', '150px'];
    tableScroll = { y: 'calc(100vh - 400px)' };
    editCache: { [key: string]: { edit: boolean; data: any } } = {};

    constructor(private fetchService: FetchService,
                private message: NzMessageService) {
    }

    ngOnInit(): void {
        this.queryInfo();
    }

    loadGateData(node: NzCascaderOption, index: number): PromiseLike<void> {
        return new Promise(resolve => {
            const params = {
                pid: index < 0 ? '0' : node.value,
                pageSize: 10000
            };
            this.fetchService
                .post('plan/wrsReportRelation/list', params)
                .subscribe(res => {
                    node.children = res['list'].map(item => {
                        if (item['menu'] === 1) {
                            return {
                                value: item.id,
                                label: item.name,
                                isLeaf: true
                            };
                        } else {
                            return {
                                value: item.id,
                                label: item.name
                            };
                        }
                    });
                    resolve();
                });
        });
    }

    onGateChanges(ids: string[]): void {
        // console.log(ids);
        if (!!ids.length) {
            this.pid = ids[ids.length - 1];
            this.fetchService
                .post('plan/wrsReportRelation/list', {
                    pid: ids[ids.length - 1],
                    pageSize: 10000
                })
                .subscribe(res => {
                    this.queryInfo();
                });
        } else {
            this.pid = null;
        }
    }

    startEdit(): void {
        this.isEditable = true;
        this.tableData.forEach(item => {
            this.editCache[item.reportId] = {
                edit: false,
                data: { ...item }
            };
        });
        Object.keys(this.editCache).forEach(key => {
            this.editCache[key].edit = true;
        });
    }

    updateEditCache(): void {
        this.tableData.forEach(item => {
            this.editCache[item.reportId] = {
                edit: false,
                data: { ...item }
            };
        });
    }

    queryInfo(): void {
        this.loading = true;
        const year = moment(this.year).format('YYYY');
        this.fetchService.post('fee/yjqFeeB/queryList', {
            pid: this.pid,
            y: year
        }).subscribe(resp => {
            this.loading = false;
            if (resp && Array.isArray(resp)) {
                this.tableData = resp;
                this.updateEditCache();
            }
        });
    }

    savePrice(): void {
        this.isEditable = true;
        this.tableData.forEach(item => {
            Object.assign(item, this.editCache[item.reportId].data);
            this.editCache[item.reportId].edit = false;
        });
        const year = moment(this.year).format('YYYY');
        this.fetchService.post('fee/yjqFeeB/batchAdd', { list: this.tableData, y: year }).subscribe(res => {
            this.isEditable = false;
            this.message.success('保存成功！');
            this.queryInfo();
        });
    }

    reset(): void {
        this.message.info('重置成功！');
        Object.keys(this.editCache).forEach((key, index) => {
            this.editCache[key].edit = false;
            this.editCache[key].data = { ...this.tableData[index] };
        });
    }

    resetCancel(): void {

    }
}

